package com.bruin_du.searcher.indexer.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class FileScanner {
    public List<File> scanFile(String rootFilePath, FileFilter filter) {
        List<File> fileList = new ArrayList<>();
        File rootFile = new File(rootFilePath);

        traversal(rootFile, filter, fileList);
        return fileList;
    }

    private void traversal(File directoryFile, FileFilter filter, List<File> fileList) {
        File[] files = directoryFile.listFiles();
        if (files == null) {
            return;
        }

        for (File file : files) {
            if (filter.accept(file)) {
                fileList.add(file);
            }
        }

        for (File file : files) {
            if (file.isDirectory()) {
                traversal(file, filter, fileList);
            }
        }
    }
}
